*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	Estimate statistical tests for balance
*	----------------------------------------------------------------------------
*	IN: 	[analysis file]
*
*	OUT:	//name of output
			local outname 		"${city}_balance.csv"
			//name of sheet in deck
			local sheetname 	"${city}_balance"
*	----------------------------------------------------------------------------

	args sample sch_res ptype bw
	tokenize `sample', parse("_")
	local years "`5'`6'`7'`8'"

*	settings

	//covariates to serve as balance outcomes
	local covs hispanic black white female fr_lunch sped ell math ela

	//pick VAM
	local olsvam OLSVAM_m_flag

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	cap log close
	log using "${log}`sheetname'.smcl", replace

*	load
	use "${builddata}${city}_analysisfile_vam`sample'`sch_res'`bw'.dta", clear

*	pscores for any offer

	gen any = 1
	set varabbrev off
	foreach sector in any{
		levelsof sch if `sector' == 1, local(`sector'_schools)
		gen pscore_`sector' = 0
		gen any_offer_`sector' = 0
		foreach sch in ``sector'_schools'{
			//pscores
			cap replace pscore_`sector' = pscore_`sector' + pscore_`ptype'_`sch'
		}
		replace pscore_`sector' = 1 if pscore_`sector' > 1
	}
	set varabbrev on

*	offered VAM balance

	ivset, ptype(`ptype')
		local S=r(S)
		local p=r(P)
		local Z=r(Z)

	//single pscore control (E[alpha]_i=sum_j(pscore_ij*alpha_j))
	levelsof sch if lottery_`ptype'_sch & `S', local(lotteryschools)

	//offered VAM and expected VAM
	foreach m in vam {
		gen pscore_`m' = 0
		set varabbrev off
		foreach sch in `lotteryschools'{
			if "`m'" == "vam"  qui su b_`olsvam' if sch == `sch'
			replace pscore_`m' = pscore_`m' + pscore_`ptype'_`sch'*r(mean)
		}
		set varabbrev on
		qui egen ind_pscore_`m' = group(pscore_`m')

		//instrument (offered VAM)
		gen offered_`m' = 0
		foreach sch in `lotteryschools'{
			if "`m'" == "vam"  qui su b_`olsvam' if sch == `sch'
			replace offered_`m' = r(mean) if offer_`sch'
		}
	}

	//running vars
	local rv
	if strpos("`ptype'","2") local rv year##c.rv_*

	//indicate any offer
	cap drop offersum
	egen offersum = rowmax(offer_*)

	// estimate
	foreach model in unc con  {
		foreach cov in `covs'{
			if "`model'"=="con" eststo any_`model'_`cov': qui ivreg2 bl_`cov' offered_vam offersum pscore_any pscore_vam `rv' i.year if `S', r
			if "`model'"=="unc" eststo any_`model'_`cov': qui ivreg2 bl_`cov' offered_vam offersum i.year, r
		}
		foreach cov in `covs' {
			local columns `columns' any_`model'_`cov'
		}
	}


***	output

	esttab2 `columns' using "${tables}/raw/`outname'", replace ///
		se mtitles stats(N) drop(rv_* *year *year* _cons, relax) nonumbers bfmt(j) se(j) sfmt(j)
	estimates clear

	insheet using "${tables}/raw/`outname'", clear nonames
	export excel using "${tables}${city}_tabs_`c(username)'_`c(current_date)'.xlsx", sheetreplace sheet("`sheetname'")

	log close
